/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.redis.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import profile.hujia.redis.dao.model.WorkItem;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * class info
 *
 * @author hujia
 * @date 2018/11/9 15:08
 */
@Slf4j
public class PutThread extends Thread {
    private RedisTest redisTest;
    private WorkItemBuild workItemBuild;
    private Integer repeatTimes;
    private CountDownLatch latch;

    public PutThread(RedisTest redisTest, WorkItemBuild workItemBuild, Integer repeatTimes, CountDownLatch latch) {
        this.redisTest = redisTest;
        this.workItemBuild = workItemBuild;
        this.repeatTimes = repeatTimes;
        this.latch = latch;
    }

    @Override
    public void run() {
        List<WorkItem> result = this.workItemBuild.createData(repeatTimes);
        ObjectMapper json = new ObjectMapper();
        try {
            for (WorkItem workItem : result) {
                redisTest.put(workItem.getId(), json.writeValueAsString(workItem));
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
        finally {
            latch.countDown();
        }
    }
}
